package com.jxaummd.aquaculture.ui.fragment.second;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.DefaultAxisValueFormatter;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
import com.jxaummd.aquaculture.R;
import com.jxaummd.aquaculture.base.BaseMainFragment;

import java.util.ArrayList;
import java.util.List;

public class SecondTabFragment extends BaseMainFragment {
    private LineChart mChartTem;
    private LineChart mChartPh;
    private LineChart mChartTur;


    public static SecondTabFragment newInstance() {
        Bundle args = new Bundle();
        SecondTabFragment fragment = new SecondTabFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frgment_second, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mChartPh = view.findViewById(R.id.fragment_second_ph);
        mChartTem = view.findViewById(R.id.fragment_second_tem);
        mChartTur = view.findViewById(R.id.fragment_second_turbidity);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        updateChart();
    }

    public void setChartStyle(LineChart mChartTemp, LineDataSet dataSet, int min, int max) {
        LineChart lineChart;
        XAxis xAxis;                //X轴
        YAxis leftYAxis;            //左侧Y轴
        YAxis rightYaxis;           //右侧Y轴
        Legend legend;              //图例

        dataSet.setLineWidth(1f);
        dataSet.setDrawFilled(true);
        dataSet.setFormLineWidth(1f);
        dataSet.setFormSize(30.f);
        dataSet.setColor(Color.GRAY);
        dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        //不显示点点
        dataSet.setDrawCircles(false);

        LineData lineData = new LineData(dataSet);

        mChartTemp.setData(lineData);
        mChartTemp.invalidate();
        mChartTemp.setBackgroundColor(Color.WHITE);
        //网格线
        mChartTemp.setDrawGridBackground(false);
        //是否显示边界
        mChartTemp.setDrawBorders(false);
        //是否可以拖动
//        mChartTemp.setDragEnabled(false);
        //是否有触摸事件
        mChartTemp.setTouchEnabled(true);

        /***XY轴的设置***/
        xAxis = mChartTemp.getXAxis();
        leftYAxis = mChartTemp.getAxisLeft();
        rightYaxis = mChartTemp.getAxisRight();

        xAxis.setDrawGridLines(false);
        rightYaxis.setDrawGridLines(false);
        leftYAxis.setDrawGridLines(true);
        leftYAxis.setAxisMaximum(max);
        leftYAxis.setAxisMinimum(min);
        leftYAxis.enableGridDashedLine(10f, 10f, 0f);
        leftYAxis.setValueFormatter(new DefaultAxisValueFormatter(43));
        rightYaxis.setEnabled(false);

        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        xAxis.setValueFormatter(new DefaultValueFormatter(12));
        //保证Y轴从0开始，不然会上移一点
        leftYAxis.setAxisMinimum(0f);

        /***折线图例 标签 设置***/
        legend = mChartTemp.getLegend();
        //设置显示类型，LINE CIRCLE SQUARE EMPTY 等等 多种方式，查看LegendForm 即可
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(12f);
        //显示位置 左下方
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);

        Drawable drawable = getResources().getDrawable(R.drawable.data_fade_blue);
        setChartFillDrawable(drawable, mChartTemp);

        //描述
        Description description = new Description();
        description.setEnabled(false);
        mChartTemp.setDescription(description);
    }

    /**
     * 设置线条填充背景颜色
     *
     * @param drawable
     */
    public void setChartFillDrawable(Drawable drawable, LineChart lineChart) {
        if (lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) {
            LineDataSet lineDataSet = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
            //避免在 initLineDataSet()方法中 设置了 lineDataSet.setDrawFilled(false); 而无法实现效果
            lineDataSet.setDrawFilled(true);
            lineDataSet.setFillDrawable(drawable);
            lineChart.invalidate();
        }
    }

//

    public void updateChart() {
//        mPresenter.getdata(1);
        //加入数据
        List<Entry> humentrys = new ArrayList<>();
        List<Entry> tementrys = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            humentrys.add(new Entry(i,  5, Math.random() * 30));
            tementrys.add(new Entry(i,  6, Math.random() * 30));
        }
        LineDataSet humdateSet = new LineDataSet(tementrys, "湿度");
        humdateSet.setLabel("湿度");
//        humdateSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> (value + "'C         ").substring(0, 6));
        LineDataSet temdataSet = new LineDataSet(humentrys, "温度");
        temdataSet.setLabel("湿度");
//        temdataSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) ->{
//                    String da = (value + "'C  ").substring(0, 6);
//                    return da;
//                }
//        );

        setChartStyle(mChartPh, temdataSet, 10, 30);
        setChartStyle(mChartTem, humdateSet, 0, 10);
        setChartStyle(mChartTur, humdateSet, 0, 10);
    }


}

